package com.yi.mall.search.service.Impl;

import com.alibaba.fastjson.JSON;
import com.yi.mall.api.SkuInfoFeign;
import com.yi.mall.common.model.search.SkuEs;
import com.yi.mall.common.utils.Result;
import com.yi.mall.search.mapper.SkuEsMapper;
import com.yi.mall.search.service.ISkuEsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SkuEsServiceImpl implements ISkuEsService {
    @Autowired
    private SkuEsMapper skuEsMapper;
    @Qualifier("com.yi.mall.api.SkuInfoFeign")
    @Autowired
    private SkuInfoFeign skuInfoFeign;
    @Override
    public void saveAll() {
        Result responseVO = skuInfoFeign.listAll();
        if(responseVO.getCode() != 2000){
            throw  new RuntimeException(responseVO.getMessage());
        }
        List<SkuEs> skuEs = JSON.parseArray(JSON.toJSONString(responseVO.getData()), SkuEs.class);
        skuEsMapper.saveAll( skuEs);
    }

    @Override
    public void save(SkuEs skuEs) {
        skuEsMapper.save(skuEs);
    }
    @Override
    public void deleteById(Long id) {
        skuEsMapper.deleteById(id);
    }
}
